package com.aliengen.thingple.update;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Environment;

public class FileUtil {
	 private String SDCARDPATH;  
	    
	    public String getSDCARDPATH() {  
	        return SDCARDPATH;  
	    }  
	  
	    public FileUtil() {  
	        SDCARDPATH = Environment.getExternalStorageDirectory() + "/";  
	    }  
	    public File creatSDFile(String fileName) {  
	        File file = new File(SDCARDPATH + fileName);  
	        return file;  
	    }  
	    public void createSDDir(String dirName) {  
	        File file = new File(SDCARDPATH + dirName);  
	        file.mkdir();  
	    }  
	    public boolean isFileExist(String fileName) {  
	        File file = new File(SDCARDPATH + fileName);  	        
	        return file.exists();  
	    }  
	    //add
	    public boolean deleteFile(String fileName){
	    	File file = new File(fileName);
	    	if(file.exists()){
	    		return file.delete();
	    	}
	    	return false;
	    }
	    public File writeToSDCard(String path, String fileName, InputStream input) {  
	        File file = null;  
	        OutputStream output = null;  
	        try {  
	            createSDDir(path);  
	            file = creatSDFile(path + fileName);  
	            output = new FileOutputStream(file);
	            byte buffer[] = new byte[1024];  
	            while (true){
	            	int size = input.read(buffer);
	            	if(size != -1) {  
	            		output.write(buffer,0,size);
	            	}else{
	            		break;
	            	}
	            }  
	            output.flush();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } finally {  
	            try {  
	                output.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	        return file;  
	    } 
}
